//프로필
function clsProfile_modify() {
    this._HTMLDom = null;
    this.listData = null;
    this.mUtil = null;
    this._init();
}

clsProfile_modify.prototype = {
    _init : function() {
        var $layer = $("section#profile_modify>section");
        this._HTMLDom = {
            "$layer" : $layer.find("form>ul"),
            "$img" : $layer.find("article.profile_area div div.p_img img")
        }
        this.mUtil = document.globalObj.util;
        this.showTime().hide();
    },
    setData : function(thisData) {
        this.listData = thisData;
        this.display();
    },
    display : function() {
        var $layer = this._HTMLDom["$layer"];
        var $img = this._HTMLDom["$img"];
        var thsiClass = this;
        
        if (this.listData["이미지"])
            $img.attr("src", this.mUtil.filePath2Url(this.listData["이미지경로"]) + this.listData["이미지"]);
        $layer.find("li:eq(0) div").html(this.listData["닉네임"]);
        $layer.find("li:eq(1) div>select>option").eq(this.listData["연령대코드"]).prop("selected", true)
        $layer.find("li:eq(1) div>select").change();
        $layer.find("li:eq(2) div>select>option").eq(this.listData["지역코드"]).prop("selected", true);
        $layer.find("li:eq(2) div>select").change();
        $layer.find("li:eq(3) div input:text").val(this.listData["직업"]);
        $layer.find("li:eq(4) div input:text").val(this.listData["핸디"]);
        $layer.find("li:eq(5) div input:text").val(this.listData["평타"]);

        this.showTime().show();
    },
    spinStart : function() {
        $.mobile.showPageLoadingMsg();
    },
    spinEnd : function() {
        $.mobile.hidePageLoadingMsg();
    },
    showTime : function() {
        return $("section#profile_modify>section");
    }
}

//데이터연동
function Profile_Comm_modify() {

    this.profileList = null;
    this.profileListRequest = null;

    this._init();
}

Profile_Comm_modify.prototype = {
    _init : function() {
        this.profileListRequest = {
            "아이디" : "sjpark"
        }
    }
    //------------------------ 프로필조회------------------------//
    ,
    getProfile : function() {
        var thisClass = this;
        var userId = this.profileListRequest["아이디"];
        

        var objJson = new MJson();
        var cm = new ConnectionManager();
        objJson.putPub(MJson.PUB.SID, "US001");
        objJson.putPub(MJson.PUB.SERVICENAME, "getMyProfile");
        objJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);

        cm.requestGet(objJson.getJsonString(), function(data) {
            var result = [];
            
            if (data.msp.contents.serviceResult == "00000") {
                var dataObj = data.msp.contents.items;
                
                //ajax

                setTimeout(function() {
                    thisClass.profileList = {
                        "아이디" : dataObj.userId,
                        "이미지경로" : dataObj.path,
                        "닉네임" : dataObj.nickname,
                        "연령대코드" : dataObj.age ? String(dataObj.age).substr(0, 1) : "",
                        "이미지" : _util.nvl(dataObj["saveFileName"])
                        //,  "연령대명": "30대"
                        ,
                        "지역코드" : dataObj.region ? dataObj.region : ''
                        //,  "지역명": "중부권(강원/충청)"
                        ,
                        "직업" : dataObj.job ? dataObj.job : '',
                        "핸디" : dataObj.handi ? dataObj.handi : 0,
                        "평타" : dataObj.average ? dataObj.average : 0
                    };

                    $(thisClass).trigger("evtFinishProfile_modify");
                }, 500);
            }
        }, function($err) {
            
        });

    },
    getProfileData : function() {
        return this.profileList;
    }
    //------------------------ 프로필조회------------------------//
    //------------------------ 프로필수정------------------------//
    , updateProfile : function(arg) {
      
        var objJson = new MJson();
        var cm = new ConnectionManager();
        
        objJson.putPub(MJson.PUB.SID, "US001");
        objJson.putPub(MJson.PUB.SERVICENAME, "updateMyProfile");
        objJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);

        objJson.putParameter("age", arg["나이"]);
        objJson.putParameter("region", arg["지역"]);
        objJson.putParameter("job", arg["직업"]);
        objJson.putParameter("handi", arg["핸디"]);
        objJson.putParameter("average", arg["평타"]);

        cm.requestGet(objJson.getJsonString(), function(data) {
            
            if (data.msp.contents.serviceResult == "00000") {
                var dataObj = data.msp.contents;
                alert("수정이 완료 되었습니다");
                $.mobile.changePage("profile_view.html");
            } else {
                alert(data.msp.result.msg);
            }

        }, function($err) {
            
            
        });
    }
    //------------------------ 프로필수정------------------------//
}

//컨트롤파일
function Profile_modify() {
    this.objComm = null;
    this.objProfile = null;

};

Profile_modify.prototype = {
    onPageShow : function() {
        /*사용자 로그인체크
        if(!common.userCheck()){
        alert("로그인 후 사용하시기 바랍니다.");
        return;
        }
        */
        
        $("a").attr("href", "").attr("data-rel", "");
        this._init();
    },
    _init : function() {
        this.objComm = new Profile_Comm_modify();
        this.objProfile = new clsProfile_modify();

        //프로필정보 조회
        this.objProfile.spinStart();
        this.objComm.getProfile();

        this._bind();
    },
    _bind : function() {
        var thisClass = this;

        //프로필정보 조회 완료이벤트
        $(this.objComm).bind("evtFinishProfile_modify", function(event) {
            var thisData = thisClass.objComm.getProfileData();

            thisClass.objProfile.setData(thisData);
            thisClass.objProfile.spinEnd();
        });
        //handi text 입력시 자동 average 증감 event
        $("#profile_modify_handi").keyup(function() {
            var handi = _$("#profile_modify_handi").val();
            if (!handi) {
                $("#profile_modify_handi").val(0);
                handi = 0;
            } else {
                $("#profile_modify_average").val(parseInt(handi) + 72);                
            }
        });
        //설정완료 버튼
        $("section#profile_modify>section>form>a").click(function(event) {
            event.preventDefault();
            
            var age = $("#profile_modify_age").val();
            var region = $("#profile_modify_region").val();
            var job = $("#profile_modify_job").val();
            var handi = $("#profile_modify_handi").val();
            var average = $("#profile_modify_average").val();
            
            var submitData = {
              "나이":  age
              , "지역": region
              , "직업": job
              , "핸디": handi
              , "평타":  average
            }
            
            thisClass.objComm.updateProfile(submitData);            
        });
        // 사진등록
        $("#profile_modify_camera").on("click", function() {
            // TODO
            try{
                var photo = new Photo();
                photo.uploadPhotoByCamera(function($result) {
                    if($result) {
                        // 이미지 업로드 성공
                        alert("내 사진 이미지 업로드 성공");
                    }
                    
                    // TODO  :: 카메라 업로드 기능 구현
                });
            }catch(ex){
              alert(1); 
            }
            
        });
        // 페이스북 연동
        $("#profile_modify_facebook").on("click", function() {
            // TODO
            alert(1);
        });
        // 개인정보 변경
        $("#profile_modify_privacy").on("click", function() {
            
            $.mobile.changePage("modify.html");
        });
        
        //이전
        $("section#profile_modify header div.top_title_G div.top_right a.btn11").click(function(event){
          event.preventDefault();
          thisClass.goNavi();
        });
    }
}
/*
 $(document).ready(function() {
 var path = String(document.location);

 if(path.indexOf("navi.html") == -1){

 document.objControl = new Profile();
 document.objControl._init();
 }
 });
 */
